package com.bailiban.qa.service;

import com.bailiban.common.entity.PageResult;
import com.bailiban.common.util.IdWorker;
import com.bailiban.qa.entity.Problem;
import com.bailiban.qa.mapper.ProblemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhulang
 * @Classname ProblemService
 * @Description ProblemService
 */
@Service
public class ProblemService {
    @Autowired
    private ProblemMapper problemMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部问题
     *
     * @return Problems
     */
    public List<Problem> findAll() {
        return problemMapper.selectList(null);
    }

    /**
     * 根据ID查询问题
     *
     * @return Problem
     */
    public Problem findById(String id) {
        return problemMapper.selectById(id);
    }

    /**
     * 增加问题
     *
     * @param problem
     */
    public void add(Problem problem) {
        //设置ID
        problem.setId(idWorker.nextId() + "");
        problemMapper.insert(problem);
    }

    /**
     * 修改问题
     *
     * @param problem
     */
    public void update(Problem problem) {
        problemMapper.updateById(problem);
    }

    /**
     * 删除问题
     *
     * @param id
     */
    public void deleteById(String id) {
        problemMapper.deleteById(id);
    }

    public PageResult findLatest(String labelid, int page, int size) {
        List<Problem> list = problemMapper.findLatestProblems(labelid, (page - 1) * size, size);
        long total = problemMapper.total(labelid);
        return new PageResult<Problem>(total, list);
    }

    public PageResult findHot(String labelid, int page, int size) {
        List<Problem> list = problemMapper.findHotProblems(labelid, (page - 1) * size, size);
        long total = problemMapper.total(labelid);
        return new PageResult<Problem>(total, list);
    }

    public PageResult findWait(String labelid, int page, int size) {
        List<Problem> list = problemMapper.findWaitProblems(labelid, (page - 1) * size, size);
        long total = problemMapper.waitTotal(labelid);
        return new PageResult<Problem>(total, list);
    }
}
